package org.springblade.mes.logistics.area.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.logistics.area.entity.InventoryArea;
import org.springblade.mes.logistics.area.mapper.InventoryAreaMapper;
import org.springblade.mes.logistics.area.model.InventoryAreaBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 服务
 *
 * @Version 1.0
 */
@Slf4j
@Service
@DS("logistics")
public class InventoryAreaService extends BaseServiceImpl<InventoryAreaMapper, InventoryArea> {

    @Transactional(rollbackFor = Exception.class)
    public boolean addData(InventoryAreaBean param) throws Exception {
        //保存
        InventoryArea entity = param;
        return this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(InventoryAreaBean param) throws Exception {
        //更新
        InventoryArea newEntity = param;
        return this.updateById(newEntity);
    }


    public InventoryAreaBean findById(Long id) {
        InventoryAreaBean cond = new InventoryAreaBean();
        cond.setId(id);
        return findBySpec(cond);
    }


    public InventoryAreaBean findBySpec(InventoryAreaBean param) {
        List<InventoryAreaBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    public List<InventoryAreaBean> findListBySpec(InventoryAreaBean param) {
        return this.baseMapper.queryList(param);
    }


    public IPage<InventoryAreaBean> findPageBySpec(IPage<InventoryAreaBean> page, InventoryAreaBean param) {
        List<InventoryAreaBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }

    /**
     * 根据编码查询
     *
     * @param no
     * @param tenantId
     * @param ctx
     * @return
     */
    public InventoryArea refreshByNo(String no, String name, String tenantId, Map<String, Object> ctx) {
        String key = "Area_" + name;
        Object v = ctx.get(key);
        if (v instanceof InventoryArea) {
            return (InventoryArea) v;
        }
        InventoryArea t;
        if (no != null && no.length() > 0) {
            LambdaQueryWrapper<InventoryArea> lambdaQueryWrapper = Wrappers.lambdaQuery(InventoryArea.class);
            lambdaQueryWrapper.eq(InventoryArea::getAreaCode, no).eq(InventoryArea::getTenantId, tenantId);
            List<InventoryArea> dataList = this.list(lambdaQueryWrapper);
            if (dataList != null && dataList.size() > 0) {
                t = dataList.get(0);
            } else {
                t = new InventoryArea();
                t.setAreaCode(no);
                t.setAreaName(name);
            }
        } else {
            LambdaQueryWrapper<InventoryArea> lambdaQueryWrapper = Wrappers.lambdaQuery(InventoryArea.class);
            lambdaQueryWrapper.eq(InventoryArea::getAreaName, name).eq(InventoryArea::getTenantId, tenantId);
            List<InventoryArea> dataList = this.list(lambdaQueryWrapper);
            if (dataList != null && dataList.size() > 0) {
                t = dataList.get(0);
            } else {
                t = new InventoryArea();
                t.setAreaCode("T" + DateUtil.format(new Date(), "yyMMddHHmmssSSS"));
                t.setAreaName(name);
            }
        }
        if (t.getId() == null) {
            t.setIsDeleted(0);
            t.setTenantId(tenantId);
            t.setStatus(0);
            t.setId(null);
            t.setCreateTime(new Date());
            t.setUpdateTime(new Date());
            this.save(t);
        }
        ctx.put(key, t);
        return t;
    }
}

